package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.entity.*;
import com.lkd.mapper.*;
import com.lkd.service.VendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionVo;
import com.lkd.vo.VmListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements VendingMachineService {

    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;

    @Override
    public List<VmListVo> getVmList(Long nodeId) {

        LambdaQueryWrapper<VendingMachine> wrapper7 = new LambdaQueryWrapper<>();
        wrapper7.eq(VendingMachine::getNodeId, nodeId);
        List<VendingMachine> vmList = getBaseMapper().selectList(wrapper7);

        List<VmListVo> vmListVos = vmList.stream().map(vendingMachine -> {
            VmListVo vmListVo = BeanUtil.copyProperties(vendingMachine, VmListVo.class);

            //nodevo
            Node node = nodeMapper.selectById(nodeId);

            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);

            //获取vmcount
            LambdaQueryWrapper<VendingMachine> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(VendingMachine::getNodeId, nodeId);
            List<VendingMachine> vendingMachineList = vendingMachineMapper.selectList(wrapper1);
            nodeVo.setVmCount(vendingMachineList.size());

            //获取region信息
            LambdaQueryWrapper<Region> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Region::getId, nodeVo.getRegionId());
            Region region = regionMapper.selectOne(wrapper2);
            //复制给regionvo
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            //多表查询出nodecount
            LambdaQueryWrapper<Node> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(Node::getRegionId, regionVo.getId());
            List<Node> nodeList = nodeMapper.selectList(wrapper3);
            //把nodecount保存到regionvo
            regionVo.setNodeCount(nodeList.size());
            //把regionvo保存到nodevo
            nodeVo.setRegion(regionVo);

            //多表查询出business
            LambdaQueryWrapper<Business> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(Business::getId, nodeVo.getBusinessId());
            //把business保存到nodevo
            nodeVo.setBusinessType(businessMapper.selectOne(wrapper4));

            LambdaQueryWrapper<VmType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VmType::getTypeId, vendingMachine.getVmType());
            VmType vmType = vmTypeMapper.selectOne(wrapper);

            vmListVo.setNodeVo(nodeVo);
            vmListVo.setType(vmType);
            return vmListVo;
        }).collect(Collectors.toList());

        return vmListVos;
    }

    @Override
    public PageBean<VmListVo> getByCode(String innerCode, Integer pageIndex, Integer pageSize) {
        Page<VendingMachine>page=new Page<>(pageIndex,pageSize);

        LambdaQueryWrapper<VendingMachine> wrapper7 = new LambdaQueryWrapper<>();
        wrapper7.like(BeanUtil.isNotEmpty(innerCode),VendingMachine::getInnerCode,innerCode);
        page= getBaseMapper().selectPage(page,wrapper7);




        List<VmListVo> vmListVos = page.getRecords().stream().map(vendingMachine -> {
            VmListVo vmListVo = BeanUtil.copyProperties(vendingMachine, VmListVo.class);

            //nodevo
            Node node = nodeMapper.selectById(vendingMachine.getNodeId());

            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);

            //获取vmcount
            LambdaQueryWrapper<VendingMachine> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(VendingMachine::getNodeId, nodeVo.getId());
            List<VendingMachine> vendingMachineList = vendingMachineMapper.selectList(wrapper1);
            nodeVo.setVmCount(vendingMachineList.size());

            //获取region信息
            LambdaQueryWrapper<Region> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Region::getId, nodeVo.getRegionId());
            Region region = regionMapper.selectOne(wrapper2);
            //复制给regionvo
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            //多表查询出nodecount
            LambdaQueryWrapper<Node> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(Node::getRegionId, regionVo.getId());
            List<Node> nodeList = nodeMapper.selectList(wrapper3);
            //把nodecount保存到regionvo
            regionVo.setNodeCount(nodeList.size());
            //把region保存到nodevo
            nodeVo.setRegion(regionVo);

            //多表查询出business
            LambdaQueryWrapper<Business> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(Business::getId, nodeVo.getBusinessId());
            //把business保存到nodevo
            nodeVo.setBusinessType(businessMapper.selectOne(wrapper4));

            LambdaQueryWrapper<VmType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VmType::getTypeId, vendingMachine.getVmType());
            VmType vmType = vmTypeMapper.selectOne(wrapper);

            vmListVo.setNodeVo(nodeVo);
            vmListVo.setType(vmType);

            return vmListVo;
        }).collect(Collectors.toList());


        return new PageBean<>(page,vmListVos);
    }
}
